package com.android.lovely.ui.adapter

import androidx.recyclerview.widget.LinearLayoutManager
import com.android.lovely.data.SearchPopular
import com.android.lovely.data.SearchRecommend
import com.android.lovely.databinding.ItemHomeTagBinding
import com.android.lovely.databinding.ItemSearchTagBinding
import com.android.lovely.databinding.LayoutSearchHistoryBinding
import com.android.lovely.databinding.LayoutSearchPopularBinding
import com.android.lovely.framework.BaseAdapter
import com.android.lovely.room.search.SearchEntity
import com.google.android.flexbox.FlexboxLayoutManager
import com.kotlin.extensions.view.ViewExt.clickable

/**
 *
 * @author why
 * @date 2025/3/13 14:24
 */
class SearchAdapter : BaseAdapter<SearchRecommend, LayoutSearchHistoryBinding>() {

    private val adapter by lazy {
        SearchHistoryAdapter()
    }

    private var onTagClickListener: onTagClickListener? = null
    fun setOnTagClickListener(onTagClickListener: onTagClickListener) {
        this.onTagClickListener = onTagClickListener
    }

    override fun bindViewWithHolderDetails(
        holder: ViewHolder,
        item: SearchRecommend?,
        binding: LayoutSearchHistoryBinding,
        position: Int
    ) {
        item ?: return
        binding.apply {
            rvHistory.layoutManager = FlexboxLayoutManager(binding.root.context)
            rvHistory.adapter = adapter
            adapter.onItemClick = { item, _ ->
                onTagClickListener?.onTagClick(item?.key ?: "")
            }
            clear.clickable {
                onTagClickListener?.removeAll()
            }
            adapter.updateList(item.list)
        }
    }
}

class SearchHistoryAdapter : BaseAdapter<SearchEntity, ItemSearchTagBinding>() {
    override fun bindViewWithHolderDetails(
        holder: ViewHolder,
        item: SearchEntity?,
        binding: ItemSearchTagBinding,
        position: Int
    ) {
        item ?: return
        binding.apply {
            tag.text = item.key
        }
    }
}

class SearchPopularAdapter : BaseAdapter<SearchPopular, LayoutSearchPopularBinding>() {

    private val adapter by lazy {
        SearchPopularItemAdapter()
    }

    private var onTagClickListener: onTagClickListener? = null
    fun setOnTagClickListener(onTagClickListener: onTagClickListener) {
        this.onTagClickListener = onTagClickListener
    }

    override fun bindViewWithHolderDetails(
        holder: ViewHolder,
        item: SearchPopular?,
        binding: LayoutSearchPopularBinding,
        position: Int
    ) {
        item ?: return
        binding.apply {
            rvHistory.layoutManager = FlexboxLayoutManager(binding.root.context)
            rvHistory.adapter = adapter
            adapter.onItemClick = { item, _ ->
                onTagClickListener?.onTagClick(item ?: "")
            }

            adapter.updateList(item.list)
        }
    }
}

class SearchPopularItemAdapter : BaseAdapter<String, ItemSearchTagBinding>() {
    override fun bindViewWithHolderDetails(
        holder: ViewHolder,
        item: String?,
        binding: ItemSearchTagBinding,
        position: Int
    ) {
        item ?: return
        binding.apply {
            tag.text = item
        }
    }
}

interface onTagClickListener {
    fun onTagClick(tag: String)
    fun removeAll()
}